package com.zj.leetcode.easy.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: algorithm
 * @description: 二叉树的最大深度
 * 104 maximum-depth-of-binary-tree
 * @author: Zhang Bo
 * @create: 2022-06-20 17:31:58
 **/
public class MaximumDepthOfBinaryTree {
    public static void main(String[] args) {
        Solution solution = new MaximumDepthOfBinaryTree().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        public int maxDepth(TreeNode root) {
            int deep = 0;
            if (root == null) {
                return deep;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);

            while (!queue.isEmpty()) {
                deep++;
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode poll = queue.poll();
                    if (poll != null) {
                        if (poll.left != null) {
                            queue.add(poll.left);
                        }
                        if (poll.right != null) {
                            queue.add(poll.right);
                        }
                    }
                }
            }
            return deep;
        }


        public int maxDepth01(TreeNode root) {

            if (root == null) {
                return 0;
            }
            int deep = 0;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode poll = queue.poll();
                    if (poll != null) {
                        if (poll.left != null) {
                            queue.add(poll.left);
                        }
                        if (poll.right != null) {
                            queue.add(poll.right);
                        }
                    }
                }
                deep++;
            }
            return deep;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
